//
//  CorgiEmoticonKeyboardView.swift
//  weibo
//
//  Created by Corgi on 17/3/30.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit

class CorgiEmoticonKeyboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func reloadPageViewData() {
        
        let indexPath = IndexPath(item: 0, section: 0)
        
        emoticonPageView.reloadItems(at: [indexPath])
    }
    
    private func setupUI() {
        
//        backgroundColor = THEMECOLOR
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        addSubview(emoticonBottomView)
        
        addSubview(emoticonPageView)
        
        addSubview(pageControl)
        
        emoticonBottomView.snp.makeConstraints { (make) in
            make.leading.bottom.trailing.equalToSuperview()
            make.height.equalTo(35)
        }
        
        emoticonPageView.snp.makeConstraints { (make) in
            make.leading.top.trailing.equalToSuperview()
            make.bottom.equalTo(emoticonBottomView.snp.top)
        }
        
        pageControl.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(emoticonPageView).offset(10)
        }
        
        emoticonBottomView.closure = { [weak self] type -> ()in
        
            switch type {
            
            case .recent:
                print("最近")
            case .normal:
                print("默认")
            case .emoji:
                print("Emoji")
            case .lxh:
                print("浪小花")
            }
            
            let indexPath = IndexPath(item: 0, section: type.rawValue - 2000)
            
            self?.emoticonPageView.scrollToItem(at: indexPath, at: .left, animated: false)
        }
        
        let indexPath = IndexPath(item: 0, section: 1)
        
        DispatchQueue.main.async {
            
            self.emoticonPageView.scrollToItem(at: indexPath, at: .left, animated: false)
        }
    }
    
    fileprivate lazy var emoticonBottomView: CorgiEmoticonBottomView = CorgiEmoticonBottomView()
    
    fileprivate lazy var emoticonPageView: CorgiEmoticonPageView = {
    
        let view = CorgiEmoticonPageView()

        view.backgroundColor = self.backgroundColor
        
        view.delegate = self
        
        view.bounces = false
        
        view.showsVerticalScrollIndicator = false
        
        view.showsHorizontalScrollIndicator = false
        
        view.isPagingEnabled = true
    
        return view
    }()
    
    fileprivate lazy var pageControl: UIPageControl = {
    
        let view = UIPageControl()
        
        view.isUserInteractionEnabled = false
        
        view.hidesForSinglePage = true
        
        view.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKey: "currentPageImage")
    
        view.setValue(UIImage(named:"compose_keyboard_dot_normal"), forKey: "pageImage")
        
        return view
    }()
}

extension CorgiEmoticonKeyboardView: UICollectionViewDelegate {

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let centerX = screenWidth * 0.5 + scrollView.contentOffset.x
        
        let center = CGPoint(x: centerX, y: 0)
        
        if let indexPath = emoticonPageView.indexPathForItem(at: center) {
        
            emoticonBottomView.setupCurrentButton(tag: indexPath.section)
            
            pageControl.numberOfPages = CorgiEmoticonTools.shared.allEmoticons[indexPath.section].count
            
            pageControl.currentPage = indexPath.item
        }
    }
}
